package org.itcast.annotationandresttemplate.ossFileUploadDownload.controller;

import org.itcast.adi.planinfo.entity.ResultBean;
import org.itcast.annotationandresttemplate.ossFileUploadDownload.service.OssService;
import org.itcast.annotationandresttemplate.ossFileUploadDownload.utils.AliOssUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@RequestMapping("/oss")
public class OssController {

    @Autowired
    private OssService ossService;
    @PostMapping("/upload")
    public ResultBean upload(@RequestParam("file") MultipartFile file) throws Exception {
        String originalFileName = file.getOriginalFilename();
        assert originalFileName != null;
        String fileName = UUID.randomUUID() +originalFileName.substring(originalFileName.lastIndexOf("."));
        String url = AliOssUtils.uploadFile(fileName,file.getInputStream());
        return ResultBean.success(url);
    }
}
